﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SqlServer.Server;

public partial class MastePage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] kategorii = { "Македонија", "Свет", "Култура", "Наука", "Спорт", "Шоу Бизнис" };
            TreeNode tn = new TreeNode();
            TreeNode tn1 = new TreeNode();
            TreeNode tn2 = new TreeNode();
            TreeNode tn3 = new TreeNode();
            TreeNode tn4 = new TreeNode();

            tn.Value = "Почетна";
            treeView.Nodes.Add(tn);
            tn1.Value = "Категории";
           
            tn3.Value = "Креирај настан";
            tn4.Value = "Мои настани";
            tn.ChildNodes.Add(tn1);
           
            tn.ChildNodes.Add(tn3);
            tn.ChildNodes.Add(tn4);

            for (int i = 0; i < 6; i++)
            {
                TreeNode pom = new TreeNode();
                pom.Value = kategorii[i];
                tn1.ChildNodes.Add(pom);
            }           
        }
        if (Session["korisnik"] != null)
        {
            lblStatus.Text = "Најавен корисник: " + Session["korisnik"].ToString();
            lnkOdjava.Visible = true;
            lblPoraka.Text = "";           
        }
        else
            lblPoraka.Text = "Мора да се најавите за да креирате настан или да ги видите Вашите настани!";

        string sqlString = "SELECT [user] " +
                           "FROM korisnik ";
        Baza baza = new Baza();
        DataSet ds = baza.Zemi(sqlString, "Korisnik");
        ViewState["Korisnici"] = ds;
        gvKorisnici.DataSource = ds;
        gvKorisnici.DataBind();
    }  
    
    protected void loginUser_Authenticate(object sender, AuthenticateEventArgs e)
    {
        string user = loginUser.UserName;
        string pass = loginUser.Password;

        string komanda = "SELECT [user], password "
                        + "FROM Korisnik "
                        + "WHERE [user]='" + user + "'";

        Baza baza = new Baza();
        DataSet data = baza.Zemi(komanda, "Korisnik");
        if (data.Tables["Korisnik"].Rows.Count==0)
        {
            loginUser.FailureText = "Погрешнo корисничко име.";
            return;
        }

        if (!data.Tables["Korisnik"].Rows[0][1].ToString().Equals(pass))
        {
            loginUser.FailureText = "Погрешна лозинка.";
            return;
        }

        lblStatus.Text = "Најавен корисник: " + user;        

        Session["korisnik"] = user;
        loginUser.FailureText = "";
        lnkOdjava.Visible = true;
        lblPoraka.Text = "";
        if (Request.Path.Equals("/DigitalnaArhiva/Registracija.aspx"))
            Response.Redirect("Pocetna.aspx");
      

    }
    protected void lnkRegistracija_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registracija.aspx");
    }
    protected void lnkOdjava_Click(object sender, EventArgs e)
    {
        lblStatus.Text = "";
        lnkOdjava.Visible = false;
        loginUser.UserName = "";
        Session.Abandon();
        Response.Redirect("Pocetna.aspx");
    }
    protected void treeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (Session["korisnik"] != null)
        {
            if (treeView.SelectedNode.Text.Equals("Креирај настан"))
            {
                lblPoraka.Text = "";
                Response.Redirect("Nastan.aspx");
            }
            if (treeView.SelectedNode.Text.Equals("Мои настани"))
            {
                lblPoraka.Text = "";
                Response.Redirect("MoiNastani.aspx");
            }
        }
        else
        {
            if (treeView.SelectedNode.Text.Equals("Креирај настан"))
            {
                if (Request.Path.Equals("/DigitalnaArhiva/Prebaruvanje.aspx") || Request.Path.Equals("/DigitalnaArhiva/Pregled.aspx"))
                    Response.Redirect("Pocetna.aspx");
                else
                Response.Redirect(Request.Path);
            }
            if (treeView.SelectedNode.Text.Equals("Мои настани"))
            {
                if (Request.Path.Equals("/DigitalnaArhiva/Prebaruvanje.aspx") || Request.Path.Equals("/DigitalnaArhiva/Pregled.aspx"))
                    Response.Redirect("Pocetna.aspx");
                else
                Response.Redirect(Request.Path);
            }
        }
        if (treeView.SelectedNode.Text.Equals("Македонија"))
            Response.Redirect("Makedonija.aspx");
        if (treeView.SelectedNode.Text.Equals("Свет"))
            Response.Redirect("Svet.aspx");
        if (treeView.SelectedNode.Text.Equals("Наука"))
            Response.Redirect("Nauka.aspx");
        if (treeView.SelectedNode.Text.Equals("Шоу Бизнис"))
            Response.Redirect("SouBiznis.aspx");
        if (treeView.SelectedNode.Text.Equals("Спорт"))
            Response.Redirect("Sport.aspx");
        if (treeView.SelectedNode.Text.Equals("Култура"))
            Response.Redirect("Kultura.aspx");
        if (treeView.SelectedNode.Text.Equals("Почетна"))
            Response.Redirect("Pocetna.aspx");
        if (treeView.SelectedNode.Text.Equals("Мои настани"))
            Response.Redirect("MoiNastani.aspx");
       
    }
    protected void imbMakedonija_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Makedonija.aspx");
    }
    protected void imbPocetna_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Pocetna.aspx");
    }
    protected void imbSvet_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Svet.aspx");
    }
    protected void imbKultura_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Kultura.aspx");
    }
    protected void imbNauka_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Nauka.aspx");
    }
    protected void imbSport_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Sport.aspx");
    }
    protected void imbSoubiznis_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("SouBiznis.aspx");
    }    
    protected void btnPrebaraj_Click(object sender, EventArgs e)
    {
        if(ddlSearch.SelectedItem.Text.Equals("Категорија"))
            Response.Redirect("Prebaruvanje.aspx?id=" + ddlKategorija.SelectedItem.Text +
            "&izbor=" + ddlSearch.SelectedItem.Text);
            else if (ddlSearch.SelectedItem.Text.Equals("Корисник"))
            {
                Response.Redirect("Prebaruvanje.aspx?id=" + txtSearch.Text +
                "&izbor=" + ddlSearch.SelectedItem.Text);
            }
                else if (ddlSearch.SelectedItem.Text.Equals("Датум"))
                {
                    string data = zemiDatum();
                    Response.Redirect("Prebaruvanje.aspx?id=" + data + "&izbor=" + ddlSearch.SelectedItem.Text);
                }
    }
    protected string zemiDatum()
    {
        string datum = calendarSearch.SelectedDate.Year.ToString() +"-";
        if (calendarSearch.SelectedDate.Month < 10)
            datum += "0";
        datum += calendarSearch.SelectedDate.Month.ToString();
        datum += "-";
        if (calendarSearch.SelectedDate.Day < 10)
            datum += "0";
        datum += calendarSearch.SelectedDate.Day.ToString();
        datum += " 00:00:00.000";        
        return datum;
    }
    protected void ddlSearch_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlSearch.SelectedIndex == 1)
        {
            txtSearch.Enabled = false;
            calendarSearch.Enabled = false;
            ddlKategorija.Enabled = true;
        }
        else if (ddlSearch.SelectedIndex == 2)
        {
            calendarSearch.Enabled = false;
            ddlKategorija.Enabled = false;
            txtSearch.Enabled = true;
            gvKorisnici.Enabled = true;
        }
        else if (ddlSearch.SelectedIndex == 3)
        {
            txtSearch.Enabled = false;
            ddlKategorija.Enabled = false;
            calendarSearch.Enabled = true;
        }
    }
    protected void calendarSearch_DayRender(object sender, DayRenderEventArgs e)
    {        
        if (e.Day.Date > calendarSearch.TodaysDate)
            e.Day.IsSelectable = false;
    }
    protected void gvKorisnici_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvKorisnici.PageIndex = e.NewPageIndex;
        gvKorisnici.SelectedIndex = -1;
        DataSet ds = (DataSet)ViewState["Korisnici"];
        gvKorisnici.DataSource = ds;
        gvKorisnici.DataBind();
    }
    protected void gvKorisnici_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(txtSearch.Enabled)
            txtSearch.Text = gvKorisnici.SelectedRow.Cells[0].Text;
    }
}
